<template>
  <div>
    <el-card
        class="box-card"
        shadow="hover"
    >
      <Pagination
          ref="pagination"
          :queryPage="loadPageData"
      >
        <div slot="search">
          <el-form
              :inline="true"
              :model="queryForm"
              size="mini"
          >
            <el-form-item label="类型">
              <el-select
                  v-model="queryForm.flowName"
                  placeholder="请选择"
              >
                <el-option
                    v-for="item in flowRuleList"
                    :key="item.name"
                    :label="item.showName"
                    :value="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="应用">
              <el-select
                  v-model="queryForm.appName"
                  placeholder="请选择"
              >
                <el-option
                    v-for="item in appList"
                    :key="item.appName"
                    :label="item.appShowName"
                    :value="item.appName"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="审核结果">
              <el-select
                  v-model="queryForm.auditResult"
                  placeholder="请选择"
              >
                <el-option
                    v-for="item in auditResultList"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                  type="primary"
                  @click="onSearch"
              >查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-table slot="table" :data="pageData" border size="mini" style="width: 100%"
        >
          <el-table-column label="#ID" prop="id" width="80"/>
          <el-table-column :formatter="formatFlowName" label="类型" prop="flowName" width="100"/>
          <el-table-column label="申请标题" prop="applyTitle" show-overflow-tooltip/>
          <el-table-column label="申请内容" prop="remark" show-overflow-tooltip/>
          <el-table-column label="应用名称" prop="appName" width="150"/>
          <el-table-column label="流程状态" prop="applyStatus.name" width="80"/>
          <el-table-column label="申请人" prop="applyUserName" width="100"/>
          <el-table-column label="申请时间" prop="created" width="150"/>
          <el-table-column label="流程步骤" prop="currFlowAudit.flowStepName" width="120"/>
          <el-table-column label="审核结果" prop="currFlowAudit.auditResult.name" width="80"/>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="small" type="text"
                         @click="goApplyDetail(scope.row)">查看
              </el-button>
              <el-button
                  v-if="scope.row.applyStatus.code=='AUDITING'&&scope.row.currFlowAudit.auditResult.code=='WAITING'"
                  size="mini" type="success" @click="submitAudit('审核通过','APPROVE',scope.row.currFlowAudit)"
              >通过
              </el-button>
              <el-button
                  v-if="scope.row.applyStatus.code=='AUDITING'&&scope.row.currFlowAudit.auditResult.code=='WAITING'"
                  size="mini" type="danger" @click="submitAudit('审核驳回','REJECT',scope.row.currFlowAudit)"
              >驳回
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </Pagination>

    </el-card>
  </div>
</template>
<script>
import {auditPageApi, flowRuleListApi, submitAuditApi} from "@/api/apply";

import Pagination from "@/components/Pagination";
import {appListApi} from "@/api/manage";
import {enumsApi} from "@/api/common";

export default {
  data() {
    return {
      queryForm: {},
      pageData: [],
      flowRuleList: [],
      appList: [],
      auditResultList: [],
      auditStatusType: {
        WAITING: "info",
        APPROVE: "success",
        REJECT: "danger",
        FINISH: "success",
        CANCEL: "warning",
      },
    };
  },
  components: {Pagination},
  methods: {
    onSearch() {
      this.$refs.pagination.go(1);
    },
    async loadPageData(current, size) {
      let query = Object.assign(
          {
            current: current,
            size: size,
          },
          this.queryForm
      );
      let data = await auditPageApi(query);
      this.pageData = data.records;
      return data;
    },
    async submitAudit(actionName, auditResult, flowAudit) {
      flowAudit.auditResult = auditResult;
      await submitAuditApi(flowAudit);
      this.$message({
        message: `提交${actionName}成功`,
        type: "success",
      });
      this.$refs.pagination.go(1);
    },
    formatFlowName(row) {
      return this.flowRuleList.filter(f => f.name == row.flowName).map(f => f.showName);
    },
    goApplyDetail(apply) {
      if (apply.flowName == 'app_deploy') {
        this.$router.push({name: 'applyDetailForm', params: {applyId: apply.id}})
      }
      if (apply.flowName == 'data_deploy') {
        this.$router.push({name: 'databaseApplyForm', params: {applyId: apply.id}})
      }
    },
  },
  async created() {
    this.flowRuleList = await flowRuleListApi();
    this.appList = await appListApi();
    this.auditResultList = await enumsApi("AuditResult");
  }
};
</script>